/**
 * @(#) WorkMediator.as
 *
 * @date		2009. 02. 25.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	근무 - Mediator
 * 				근무관리 Presentation Logic을 관리 
 */
package com.skt.common_hr.mintview.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.calendar.Holiday;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.mintview.MintViewFacade;
	import com.skt.common_hr.mintview.events.NoticeEvent;
	import com.skt.common_hr.mintview.events.SelMemberEvent;
	import com.skt.common_hr.mintview.events.SelOrgEvent;
	import com.skt.common_hr.mintview.model.MemberSearchProxy;
	import com.skt.common_hr.mintview.model.MintViewListProxy;
	import com.skt.common_hr.mintview.model.OrgProxy;
	import com.skt.common_hr.mintview.model.vo.DateVO;
	import com.skt.common_hr.mintview.model.vo.MemberVO;
	import com.skt.common_hr.mintview.view.components.MintViewOverView;
	import com.skt.common_hr.work_sac.model.TeamVacationProxy;
	import com.skt.common_hr.work_sac.model.vo.TeamVacationVO;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class MintViewMediator extends OverViewMediator
	{
		public static const NAME:String = "MintViewMediator"; 
		
		private var selectedMember:MemberVO;
		private var currentMonth:Date = new Date();

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function MintViewMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
 			preloadData();
 			checkAuthority(Variables.authorities);
 			super.initializeView();
 		}
 		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			view.vsMintView.selectedIndex = 0;
 			loadTeamMember(Variables.myOrgCode);
 			view.setDateRange();
 			view.txtOrg.text = Variables.myOrg;
 			view.txtOrg.data = Variables.myOrgCode;
 			loadListData();
 		}
 		
 		override protected function closedView():void
 		{
 			removeEventListeners();
 		}
 		
 		private function loadListData(event:Event = null):void{
 			var mintViewListProxy:MintViewListProxy = facade.retrieveProxy(MintViewListProxy.NAME) as MintViewListProxy;
 			var codeStr:String = "";
 			if(view.cbSearchgb.selectedItem == null){
 				codeStr = "";
 			}else{
 				codeStr = view.cbSearchgb.selectedItem.data;
 			}
 			
 			mintViewListProxy.load(Variables.myPernr, view.sDay, view.eDay, codeStr, view.keyWord.text, view.txtOrg.data.toString());	
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(MintViewOverView.MintViewListData, loadListData);
 			view.addEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
 			view.addEventListener(SelOrgEvent.SELECTED_ORG, onSelectedOrg);
 			view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
 			
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(MintViewOverView.MintViewListData, loadListData);
 			view.removeEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
 			view.removeEventListener(SelOrgEvent.SELECTED_ORG, onSelectedOrg);
 			view.removeEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
 		}
 		
		private function preloadData():void
 		{
 			var orgProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
 			orgProxy.load(Variables.myPernr, "X");
 			
 		}
 		
 		[Bindable] public var historyIdx:int;
 		private function onDetailView(event:NoticeEvent):void
		{
 			var reqMediator:NoticeWriteMediator = facade.retrieveMediator(NoticeWriteMediator.NAME) as NoticeWriteMediator;
 			if(!reqMediator) {
			    reqMediator = new NoticeWriteMediator(view.noticeWrite);
			    facade.registerMediator(reqMediator);
		    }
		  	
	  		if(reqMediator) facade.sendNotification(MintViewFacade.WRITE_SEARCH_NOTICE, event.selectedItem);	
		    
			view.vsMintView.selectedIndex = 2;
			historyIdx = event.historyIdx;
//			view.noticeWrite.dataProvider = event.selectedItem;
		}
		
		private function onDetailNewView():void
		{
 			var reqMediator:NoticeWriteMediator = facade.retrieveMediator(NoticeWriteMediator.NAME) as NoticeWriteMediator;
 			if(!reqMediator) {
			    reqMediator = new NoticeWriteMediator(view.noticeWrite);
			    facade.registerMediator(reqMediator);
		    }
		  	
	  		if(reqMediator) facade.sendNotification(MintViewFacade.MODE_WRITE_NOTICE, view.proVoData);	
		    
			view.vsMintView.selectedIndex = 2;
			historyIdx = 1;
//			view.noticeWrite.dataProvider = event.selectedItem;
		}
		
		
 		private function checkAuthority(authList:ArrayCollection):void
 		{
 			for each(var vo:* in authList)
 			{
 				if(vo.EMENU == "ALTERREQWORK") {
 					view.registable = true;
 					return;
 				}
 			}
 			view.registable = false;
 		}
 		
 		private var _pernr:String;
 		private function onSelectedMember(event:SelMemberEvent):void
 		{
			selectedMember = event.member;
		    
		    var reqMediator:ProfileMediator = facade.retrieveMediator(ProfileMediator.NAME) as ProfileMediator;
		    
		    if(!reqMediator) {
		    reqMediator = new ProfileMediator(view.privateDetail);
		     facade.registerMediator(reqMediator);
		    }
		    if(reqMediator) facade.sendNotification(MintViewFacade.CHANGE_SELECTED_MEMBER, event.pernr);
			view.vsMintView.selectedIndex = 1; 
			_pernr = event.pernr;
 		}
 		
 		private function onSelectedOrg(event:SelOrgEvent):void
 		{
			loadTeamMember(event.orgCode); 		 
			loadListData();
			
//			
// 			var mintViewListProxy:MintViewListProxy = facade.retrieveProxy(MintViewListProxy.NAME) as MintViewListProxy;
// 			var codeStr:String = "";
// 			if(view.cbSearchgb.selectedItem == null){
// 				codeStr = "";
// 			}else{
// 				codeStr = view.cbSearchgb.selectedItem.data;
// 			}			
//			mintViewListProxy.load(Variables.myPernr, view.sDay, view.eDay, codeStr, view.keyWord.text, event.orgCode);	
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					MintViewFacade.LOAD_MEMBERSEARCH_COMPLETE,
					MintViewFacade.LOAD_MEMBERSEARCH_FAILED,
					MintViewFacade.LOAD_LIST_NEW_VIEW,
					MintViewFacade.LOAD_NAMESEARCH_COMPLETE,
					MintViewFacade.LOAD_NAMESEARCH_FAILED,
					MintViewFacade.LOAD_ORGLIST_COMPLETE,
					MintViewFacade.LOAD_ORGLIST_FAILED,
					MintViewFacade.CLOSED_REQUEST_VIEW,
					MintViewFacade.LOAD_CODELIST_COMPLETE,
					MintViewFacade.LOAD_CODELIST_FAILED
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				// 2011.1223 아래 두개 추가 
				case MintViewFacade.LOAD_NAMESEARCH_COMPLETE:
					view.memberList = (note.getBody() as MemberSearchProxy).memberList;
					commonFacade.setStatusMessage("STATUS_SELECT_MEMBER");
					break;
				case MintViewFacade.LOAD_NAMESEARCH_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as MemberSearchProxy).msgStr);
					break;
				case MintViewFacade.LOAD_LIST_NEW_VIEW:
					onDetailNewView();
					break;	
				
				case MintViewFacade.LOAD_ORGLIST_COMPLETE:
					view.orgList = (note.getBody() as OrgProxy).orgList;
					break;
				case MintViewFacade.LOAD_ORGLIST_FAILED:
					break;
				case MintViewFacade.LOAD_CODELIST_COMPLETE:
					view.pageNavigator.TotalPageCount = (note.getBody() as MintViewListProxy).rowCount;	//총페이지
					view.dataProvider = (note.getBody() as MintViewListProxy).mintListArr;
					
					if(view.comboBool){
						view.cbSearchgb.dataProvider = (note.getBody() as MintViewListProxy).comboScodeArr;
						view.comboBool = false;	
					}
					
					break;
				case MintViewFacade.LOAD_CODELIST_FAILED:
					break;
				case MintViewFacade.CLOSED_REQUEST_VIEW:
					if(historyIdx == 0){
						view.vsMintView.selectedIndex = 0;	
						loadListData();
					}else{
						view.vsMintView.selectedIndex = 1;
						facade.sendNotification(MintViewFacade.CHANGE_SELECTED_MEMBER, _pernr);
						historyIdx = 0;
						
						// 수정 ..20130628
						loadListData();
					}
					
					break;			
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		private function selectResult(memberArr:ArrayCollection):void{
			view.memberList = memberArr;
			//setStatusMessage("데이터가 정상적으로 로드되었습니다.");
		}
		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		// 본인 기록은 제외
		private function getTeamList(proxy:TeamVacationProxy):ArrayCollection
		{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var vo:TeamVacationVO in proxy.teamVacationList) {
				trace(Variables.myPernr);
				if(vo.PERNR!=Variables.myPernr) {
					arr.addItem(vo);
				}
			}
			return arr;
		}
		
		//구성원 정보 로드.
		private function loadTeamMember(code:String):void
 		{
 			var proxy:MemberSearchProxy = facade.retrieveProxy(MemberSearchProxy.NAME) as MemberSearchProxy;
 			proxy.load(code, "");
 		}
				
		private function clearVacationList():void
		{
//			view.vacationList = new ArrayCollection();
		}
		

		private function toCalendarHoliday(src:ArrayCollection):ArrayCollection
		{
			var coll:ArrayCollection = new ArrayCollection();
			for each(var rec:DateVO in src) {
				var vo:Holiday = new Holiday();
				vo.DATUM = new Date(rec.DATUM);
				vo.DTYPE = rec.DTYPE;
				vo.DTEXT = rec.DTEXT;
				coll.addItem(vo);
			}
			return coll;
		}
		
		
		private function deletableDay(date:Date):Boolean
		{
			var today:Date = DateUtil.todayNoTime();
			today.date = 1;
			today.date--;
			today.date = 1;
			return (date > today) ? true : false; 
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():MintViewOverView
        {
            return viewComponent as MintViewOverView;
        }
	}
}